<style>
.dragStyle{
	background-color:#afba9b;
}
</style>
<script type="text/javascript" src="assets/js/jquery.tablednd.js"></script>
<script type="text/javascript">
var aHoles = new Array();

function holeEdit(e){
	if(e.which == 13){
		
	}
}

function onblur(e){
	//debugger;
	var tbody = $(this).parent().parent().parent();
	var b_id = extractID($(tbody).attr("id"));
	$("#holeDisp_" + h_id).show();
	$("#holeEdit_" + h_id).hide();
}

$(document).ready(function(){
	
	$("#courseForm").ajaxForm({beforeSubmit:validate});

	$("input[name=txtBxHoleName]").keypress(holeEdit);
	//$("input[name=txtBxHoleName]").blur(onblur);
	$("input[name=txtBxPar]").keypress(holeEdit);
	//$("input[name=txtBxPar]").blur(onblur);
	
	$("#tableHoleList tbody tr td").not("[name=ordercell]").dblclick(function(){
		var td = $(this);
		var h_id = extractID(td.parent().parent().attr("id"));

		var OriginalContent = td.text().trim();

		var tbody_disp = "#holeDisp_" + h_id;
		var tbody_edit = "#holeEdit_" + h_id;
		
		$(tbody_disp).hide();
		$(tbody_edit).show();
		
		

		$(this).children().first().keypress(function(e){
				debugger;
				if(e.which == 13){
					debugger;
					if($(this).val().trim() == ""){
						alert("This cell must not be left blank.");
						$(this).parent().text(OriginalContent);
					}

					var holeId = $(this).parent().parent().find("input[name=hdnHoleId]").val();
					var aSib = $(this).parent().siblings();
					debugger;
					var newContent = $(this).val().trim();
					$(this).parent().text(newContent);
					$(this).parent().removeClass("cellEditing");

					//aHoles[holeId] = newContent;
					
				}
			});

		$(this).children().first().blur(function(){
				$(this).parent().text(OriginalContent);
				//$(this).parent().removeClass("cellEditing");
			});
	});
	
});

function validate(formData,jqForm, options){
	debugger;
	var form = jqForm[0];
	var aErrors = new Array();

	if(!form.txtBxCourseName.value)
		aErrors.push("* Course name must not be blank.");

	if(!form.ddVenueList.value)
		aErrors.push("* Venue must be selected");

	var sErrorMsg = aErrors.join("\n");

	if(sErrorMsg != "")
		alert(sErrorMsg);

	return (!(sErrorMsg != ""));
}

function extractID(e){
	var i = e.indexOf("_");
	return h_id = e.substr(i+1,e.length-i-1);
}

</script>

<h3 style="margin:0">Course Management</h3>
<?php printf("<a href=\"?%s\">&lt; Back to the list</a>",AdminItemEnum::COURSES);?>
<form id="courseForm" name="courseForm" action="index.php" method="POST">
<input type="hidden" name="<?php echo AdminItemEnum::COURSES?>"/>
<input type="hidden" name="edit"/>
<input type="hidden" name="courseid" value="<?php echo $courseid;?>"/>
<table width="100%" cellspacing="0" cellpadding="1">
	<tr style="background-color:#afba9b">
		<td align="right" colspan="2">
			<input type="button" name="btnCancelCourseEdit" value="Cancel" onclick="location.href='?<?php echo AdminItemEnum::COURSES;?>'"/>
			<input type="button" name="btnChangeOrder" value="Change Order" onclick="location.href='?<?php echo AdminItemEnum::COURSES;?>'"/>
			<input type="button" name="btnSaveCourse" value="Save"/>
		</td>
	</tr>
	<tr>
		<td align="right" width="10%">Course Name:</td>
		<td><input type="text" id="txtBxCourseName" name="txtBxCourseName" value="<?php echo $course->coursename;?>"/>
	</tr>
	<tr>
		<td align="right">Venue:</td>
		<td>
			<select id="ddVenueList" name="ddVenueList">
				<option value="">Select</option>
				<?php 
					foreach($venuelist as $v){
						$selected = ($v->venueid == $course->venue->venueid)?" selected":"";
						printf("<option value=\"%s\"%s>%s</option>",$v->venueid,$selected,$v->venuename);
					}	
				?>
			</select>	
		</td>
	</tr>
	<tr>
		<td align="right" valign="top">Holes:</td>
		<td valign="top">
			<table id="tableHoleList" width="100%" border="1" class="resultgrid">
				<tr>
					<th>Hole Name</th>
					<th>Par</th>
					<th>Order</th>
				</tr>
				<?php
					$c = 0;
					foreach ($holelist as $h){
						printf("<tbody id=\"holeDisp_%s\">",$h->holeid); 
						$rowClass = ($c%2==1)?" class=\"altrow\"":"";
						printf("<tr%s>",$rowClass);
						printf("	<td>%s</td>",trim($h->holename));
						printf("	<td>%s</td>",$h->par);
						printf("	<td name=\"ordercell\">%s",$h->holeorder);
						printf("<input type=\"hidden\" id=\"hdnHoleId\" name=\"hdnHoleId\" value=\"%s\"/></td>",$h->holeid);
						echo "</tr>\n";
						echo "</tbody>";
						printf("<tbody id=\"holeEdit_%s\" style=\"display:none\">",$h->holeid);
						echo "		<td>";
						printf("		<input type=\"text\" name=\"txtBxHoleName\" value=\"%s\"/>",$h->holename);
						printf("		<input type=\"hidden\" name=\"hdnHoleName\" value=\"%s\"/>",$h->holename);
						echo "		</td>";
						echo "		<td>";
						printf("		<input type=\"text\" name=\"txtBxPar\" size=\"1\" maxlength=\"1\" value=\"%s\"/>",$h->par);
						printf("		<input type=\"hidden\" name=\"hdnPar\" value=\"%s\"/>",$h->holeid,$h->par);
						echo "		</td>";
						printf("	<td name=\"ordercell\">%s</td>",$h->holeorder);
						echo "</tbody>";
						$c++;
					}
				?>
			</table>
		</td>
	</tr>
	<tr>
		<td align="right" colspan="2">
			<input type="button" name="btnCancelCourseEdit" value="Cancel" onclick="location.href='?<?php echo AdminItemEnum::COURSES;?>'"/>
			<input type="button" name="btnChangeOrder" value="Change Order" onclick="location.href='?<?php echo AdminItemEnum::COURSES;?>'"/>
			<input type="button" name="btnSaveCourse" value="Save"/>
		</td>
	</tr>
</table>
</form>